<script setup>
import { useRoute, useRouter } from "vue-router";
import { onBeforeMount, reactive, ref } from "vue";
import {
  categoryNav as categoryNavApi,
  goodsList as goodsListApi,
} from "@/api/category";
const route = useRoute();
const router = useRouter();
const id = ref(route.query.id);
const state = reactive({
  currentNav: {},
  navData: [],

  goodsList: [],
});
const active = ref("");
// console.log("id=>", id);
const getNavList = async () => {
  const res = await categoryNavApi({ id: id.value });
  state.navData = res.navData;
  state.currentNav = res.currentNav;
  active.value = res.currentNav.name;
};
// 获取商品列表
const getGoodsList = async () => {
  const res = await goodsListApi({ categoryId: id.value });
  state.goodsList = res.data;
  active.value = res.currentNav.name;
  state.currentNav = res.currentNav;
};
onBeforeMount(() => {
  getNavList();
  getGoodsList();
});
// 返回上一页
const goBack = () => {
  router.go(-1);
};
// tab点击事件
const handeTabClick = ({ name, title }) => {
  //修改ID
  id.value = state.navData.find((item) => item.name == name).id;
  getGoodsList();
};
// 跳转到商品详情页面
const goDetail = (id) => {
  router.push({ path: "/goodsdetail", query: { id } });
};
</script>

<template>
  <div class="sub-category-page">
    <van-sticky>
      <div>
        <NavBar @leftclick="goBack" :left-arrow="true" title="分类"></NavBar>
      </div>
      <div class="nav-list-container">
        <van-tabs v-model:active="active" @click-tab="handeTabClick">
          <van-tab
            :name="item.name"
            :title="item.name"
            v-for="item in state.navData"
            :key="item.id"
          ></van-tab>
        </van-tabs>
      </div>
    </van-sticky>

    <div class="banner-container">
      <div class="title">{{ state.currentNav.name }}</div>
      <div class="sub-title">{{ state.currentNav.front_desc }}</div>
    </div>

    <!-- 商品列表 -->
    <div class="product-container" v-if="state.goodsList.length">
      <div
        class="product-item"
        v-for="product in state.goodsList"
        :key="product.id"
        @click="goDetail(product.id)"
      >
        <van-image class="product-img" :src="product.list_pic_url" />
        <div class="product-name">
          {{ product.name }}
        </div>
        <div class="product-price">¥{{ product.retail_price }}</div>
      </div>
    </div>
    <div v-else class="no-data">数据库暂无数据...</div>
  </div>
</template>

<style lang="scss" scoped>
@use "@/assets/common.scss" as *;
.sub-category-page {
  .nav-list-container {
    :deep(.van-tabs__line) {
      background: #cea62a;
    }
  }
  .banner-container {
    width: 710px;
    height: 150px;
    background: #f9f4e5;
    border: 1px solid rgba(235, 220, 173, 1);
    margin: 0 auto;
    margin-top: 26px;
    text-align: center;
    .title {
      margin-top: 29px;
      font-family: PingFangSC-Medium;
      font-size: 32px;
      color: #000000;
      font-weight: 500;
    }
    .sub-title {
      margin-top: 4px;
      font-family: PingFangSC-Regular;
      font-size: 28px;
      color: #666666;
    }
  }
  .product-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    padding: 0 20px;
    margin-top: 26px;
    .product-item {
      margin-bottom: 44px;
      .product-img {
        width: 350px;
        height: 350px;
        background-color: #fff;
      }
      .product-name {
        @include textOverFlow;
        margin-top: 17px;
        width: 350px;
        font-family: PingFangSC-Medium;
        color: #333333;
        font-weight: 500;
        font-size: 26px;
      }
      .product-price {
        margin-top: 5px;
        font-family: PingFangSC-Regular;
        color: #9e4242;
        font-size: 26px;
      }
    }
  }
  .no-data {
    text-align: center;
    margin-top: 122px;
    font-family: PingFangSC-Regular;
    font-size: 32px;
    color: #999999;
  }
}
</style>
